package com.common.library.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.common.library.R;
import com.common.library.listener.OnAppListener;
import com.common.library.ui.view.LoadingScaleView;

/**
 * @author txf
 * @Title 碎片基类
 * 1.提供对外接口 OnListener
 * 2.提供loading视图
 * @package com.txf.prevention.ui.fragment
 * @date 2017/5/3 0003
 */

public abstract class BaseFragment extends Fragment {
    protected String TAG = getClass().getName();
    private OnAppListener.OnfragListener l;
    private LoadingScaleView mLoadingScaleView;

    public OnAppListener.OnfragListener getOnListener() {
        return l;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnAppListener.OnfragListener)
            l = (OnAppListener.OnfragListener) context;
    }
    public <E extends View> E getViewById(@IdRes int id) {
        return (E) getView().findViewById(id);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, container, false);
        FrameLayout base_fragment_content = (FrameLayout) view.findViewById(R.id.base_fragment_content);
        base_fragment_content.addView(buildChildView(), new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        base_fragment_content.addView(buildLoadingView(), new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        return view;
    }

    protected abstract View buildChildView();

    protected View buildLoadingView() {
        mLoadingScaleView = new LoadingScaleView(getContext());
        mLoadingScaleView.InterceptClick(true);
        hideLoadingView();
        return mLoadingScaleView;
    }

    public LoadingScaleView getLoadingScaleView() {
        return mLoadingScaleView;
    }

    protected void showLoadingView() {
        if (mLoadingScaleView.getVisibility() != View.VISIBLE)
            mLoadingScaleView.setVisibility(View.VISIBLE);
    }

    protected void hideLoadingView() {
        if (mLoadingScaleView.getVisibility() == View.VISIBLE)
            mLoadingScaleView.setVisibility(View.INVISIBLE);
    }
}
